home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Audio / Patchmix / Source / Buzz.m < prev    next >
Text File  |  1992-08-01  |  6KB  |  240 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Buzz.h"
  5. #import "Instrum.h"
  6. #import <appkit/graphics.h>
  7. #import "Statement.h"
  8.  
  9. @implementation Buzz
  10.  
  11. + initialize
  12. {
  13.        image = [NXImage findImageNamed:"Buzz"];
  14.     ampOffset.x = 17;
  15.     ampOffset.y = 56;
  16.     freqOffset.x = 38;
  17.     freqOffset.y = 56;
  18.     nharOffset.x = 57;
  19.     nharOffset.y = 46;
  20.     outOffset.x = 29;
  21.     outOffset.y = 9;
  22.     
  23.     return self;
  24. }
  25.  
  26. - init
  27. {
  28.     written = NO;
  29.     size.width = 80.;
  30.     size.height = 80.;
  31.     center.x = size.width/2.;
  32.     center.y = size.height/2.;
  33.     strcpy(type,"Buzz");
  34.     strcpy(name,"buzz");
  35.     
  36.     amp = [[Param alloc] init:self :&Offset];
  37.     freq = [[Param alloc] init:self :&freqOffset];
  38.     nhar = [[Param alloc] init:self :&nharOffset];
  39.     out = [[Param alloc] init:self :&outOffset];
  40.  
  41.     [freq setValue:"440"];    // set non-"1" default values
  42.     [nhar setValue:"8"];        
  43.     [amp setTitle:"Amp:"];
  44.     [freq setTitle:"Freq:"];
  45.     [nhar setTitle:"Nhar:"];
  46.     [out setTitle:"Out:"];
  47.     
  48.     paramList = [[List alloc] initCount:4];
  49.     [paramList addObject:amp];
  50.     [paramList addObject:freq];
  51.     [paramList addObject:nhar];
  52.     [paramList addObject:out];
  53.     
  54.     [Inst putUgenInList:self];
  55.     
  56.     return self;
  57. }
  58.  
  59. - remove
  60. {
  61.     id cp,param;
  62.     int i;
  63.                             // make sure not connected first 
  64.     for(i = 0; i < ([paramList count]); i++) {    
  65.         param = [paramList objectAt:i];
  66.         if(cp = [param getConnectedParam]) 
  67.             return 0;
  68.     }
  69.                             // remove ugen from list
  70.     [Inst removeUgenFromList:self];
  71.     return self;
  72. }
  73.  
  74. - (NXImage *)getImage
  75. {
  76.     return image;
  77. }
  78.  
  79. - move:(NXPoint *)newloc
  80. {
  81.     location = *newloc;
  82.     [amp move:&location];
  83.     [freq move:&location];
  84.     [nhar move:&location];
  85.     [out move:&location];
  86.     return self;
  87. }
  88.  
  89. - findParamAtPoint:(NXPoint *)point
  90. {
  91.     NXRect *rect;
  92.     int i;
  93.     id param;
  94.     
  95.     for(i = 0; i < [paramList count]; i++) {
  96.         param = [paramList objectAt:i];
  97.         rect = [param getRect];
  98.         if(NXMouseInRect(point,rect,NO))
  99.             return param;
  100.     }
  101.     //printf("no param found\n");
  102.     return nil;
  103. }
  104.  
  105. - writeUgen
  106. {
  107.     BOOL am = NO;
  108.     BOOL rm = NO;
  109.     char ampVar[10];
  110.     char freqVar[10];
  111.     /*     for each input param:
  112.             see if there is input ugen
  113.                 if so, grab the output (ug?) and multiply it by this
  114.                     param value
  115.                 if not just use the param value
  116.          after all params done, write relevant code into lists
  117.             (declarations, assignments and loop statements)
  118.             i.e. ug? = oscil(,,,,);
  119.         go to output param connected ugen and call its "writeUgen"
  120.     */
  121.     int i;
  122.     int parent[3];
  123.     id param;
  124.     id ug;
  125.     id nupar;
  126.     char str[50];
  127.     char par[20];
  128.     
  129.     //printf("writing ugen %d\n",index);
  130.     sprintf(str,"\tfloat ug%d;\n",index);
  131.     [Inst putVarInList:str];
  132.     sprintf(str,"\tfloat amp%d;\n",index);
  133.     [Inst putVarInList:str];
  134.     sprintf(str,"\tint wave%d;\n",index);
  135.     [Inst putVarInList:str];
  136.     sprintf(str,"\tfloat si%d;\n",index);
  137.     [Inst putVarInList:str];
  138.     sprintf(str,"\tfloat nhar%d;\n",index);
  139.     [Inst putVarInList:str];
  140.     sprintf(str,"\tfloat *f%d;\n",index);
  141.     [Inst putVarInList:str];
  142.     sprintf(str,"\tfloat phase%d;\n",index);
  143.     [Inst putVarInList:str];
  144.     sprintf(str,"\tfloat out%d;\n",index);
  145.     [Inst putVarInList:str];
  146.  
  147.     for(i = 0; i < ([paramList count]-1); i++) {    
  148.         param = [paramList objectAt:i];
  149.         if(nupar = [param getConnectedParam]) {
  150.             ug = [nupar getUgen];
  151.             if([ug getWritten] == NO) { 
  152.                 parent[i] = [[ug writeUgen] getIndex];
  153.             }
  154.             else  {
  155.                 parent[i] = 0;
  156.             }
  157.             //printf("parent[%d] = %d\n",i,parent[i]);
  158.         }
  159.         else 
  160.             parent[i] = 0;
  161.         
  162.     }
  163.     sprintf(str,"\twave%d = 1;\n",index); // wave hardwired to function 1
  164.     [Inst putAssignInList:str];
  165.     
  166.     if(parent[0]) {     // amplitude input ugen    
  167.         //printf("buzz amp input ugen\n");
  168.         rm = YES;
  169.         sprintf(str,"\tamp%d = %.2f;\n",index, 
  170.             atof([[paramList objectAt:0] getValue]));
  171.         [Inst putAssignInList:str];
  172.         sprintf(str,"\tfloat amprm%d;\n",index);
  173.         [Inst putVarInList:str];
  174.         sprintf(str,"\t\tamprm%d = amp%d * ug%d;\n",index,  
  175.             index,parent[0]);
  176.         [Inst putLoopInList:str];
  177.     }
  178.     else {                // amp preset or default
  179.         //printf("amp preset or pfield\n");
  180.         strcpy(par,[[paramList objectAt:0] getValue]);
  181.         if(par[0] == 'p' && par[1] == '[')
  182.             sprintf(str,"\tamp%d = %s;\n", index, par);
  183.         else
  184.             sprintf(str,"\tamp%d = %.2f;\n",index,atof(par));
  185.         [Inst putAssignInList:str];
  186.     }
  187.     if(parent[1]) {
  188.         if(!strcmp([[[freq getConnectedParam] getUgen] getType],"Converter")) { 
  189.             strcpy(par,[[paramList objectAt:1] getValue]);
  190.             sprintf(str,"\tsi%d = ug%d*%.2f*fsize(1)/SR;\n", index, parent[1], atof(par));
  191.         }
  192.         else {    // fm doesn't work
  193.             sprintf(str,"\tsi%d = %.2f*fsize(1)/SR;\n", index, atof(par));
  194.         }
  195.         [Inst putAssignInList:str];
  196.     }
  197.     else {                 // freq preset
  198.         //printf("freq preset or pfield\n");
  199.         strcpy(par,[[paramList objectAt:1] getValue]);
  200.         if(par[0] == 'p' && par[1] == '[')
  201.             sprintf(str,"\tsi%d = %s*fsize(1)/SR;\n", index, par);
  202.         else
  203.             sprintf(str,"\tsi%d = %.2f*fsize(1)/SR;\n", index, atof(par));
  204.         [Inst putAssignInList:str];
  205.     }
  206.     
  207.     strcpy(par,[[paramList objectAt:2] getValue]);
  208.     if(par[0] == 'p' && par[1] == '[')
  209.         sprintf(str,"\tnhar%d = %s;\n",index,par);
  210.     else
  211.         sprintf(str,"\tnhar%d = %.2f;\n", index, atof(par));
  212.     [Inst putAssignInList:str];
  213.     sprintf(str,"\tf%d = (float *)floc(wave%d);\n",index,index);
  214.     [Inst putAssignInList:str];
  215.     sprintf(str,"\tphase%d = 0.;\n",index);
  216.     [Inst putAssignInList:str];
  217.     
  218.     if(rm)
  219.         sprintf(ampVar,"amprm%d",index);
  220.     else
  221.         sprintf(ampVar,"amp%d",index);
  222.     sprintf(freqVar,"si%d",index);
  223.         
  224.                     // output amplitude multiplier:
  225.     strcpy(par,[[paramList objectAt:3] getValue]);
  226.     if(par[0] == 'p' && par[1] == '[')
  227.         sprintf(str,"\tout%d = %s;\n",index,par);
  228.     else
  229.         sprintf(str,"\tout%d = %.2f;\n", index, atof([[paramList objectAt:3] getValue]));
  230.     [Inst putAssignInList:str];
  231.  
  232.     sprintf(str,"\t\tug%d = %s(%s,%s,nhar%d,f%d,&phase%d)*out%d;\n", index,name, ampVar,freqVar,index,index,index,index);
  233.     
  234.     [Inst putLoopInList:str];
  235.     written = YES;
  236.     return self;    
  237. }
  238.  
  239. @end
  240.